package com.mycompany.services.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class AbstractHibernateDAO<DomainObject extends Serializable, KeyType extends Serializable> extends HibernateDaoSupport {

	protected Class<DomainObject> domainClass = getDomainClass();
	
	protected abstract Class<DomainObject> getDomainClass();

	@SuppressWarnings("unchecked")
	public DomainObject load(KeyType id) {
		return (DomainObject) getHibernateTemplate().load(getDomainClass(), id);
	}

	public void update(DomainObject object) {
		getHibernateTemplate().update(object);
	}

	public void save(DomainObject object) {
		getCurrentSession().save(object);
		//getHibernateTemplate().save(object);
	}

	private org.hibernate.classic.Session getCurrentSession() {
		return getSessionFactory().getCurrentSession();
	}

	public void delete(DomainObject object) {
		getHibernateTemplate().delete(object);
	}

	public void deleteById(KeyType id) {
		Object obj = load(id);
		getHibernateTemplate().delete(obj);
	}

	@SuppressWarnings("unchecked")
	public List<DomainObject> getList() {
		return (getHibernateTemplate().find("from " + domainClass.getName() + " o"));
	}

	public void deleteAll() {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				String hqlDelete = "delete " + domainClass.getName();
				session.createQuery(hqlDelete).executeUpdate();
				return null;
			}

		});

	}

	@SuppressWarnings("rawtypes")
	public long count() {
		List list = getHibernateTemplate().find("select count(*) from " + domainClass.getName() + " o");
		Long count = (Long) list.get(0);
		return count.intValue();
	}

}